package com.javaking.clanteam.studentutils.courses.assingments;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.javaking.clanteam.studentutils.courses.CourseData;
import com.javaking.clanteam.studentutils.sql.AssignmentHelper;
import com.javaking.clanteam.studentutils.sql.CourseHelper;

public class AssignmentData {
	
	public enum Priority {
		LOW,
		NORMAL,
		HIGH,
		URGENT
	}
	
	private Map<String,Object> mData;

	private CourseData mParentCourse;
	
	
	/**
	 * @return the parentCourse
	 */
	public CourseData getParentCourse() {
		return mParentCourse;
	}

	/**
	 * @return the assigned date
	 */
	public Date getAssigned() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(Long.parseLong(mData.get(AssignmentHelper.COLUMN_ASSIGNED).toString()));
		return cal.getTime();
	}

	/**
	 * @return the dueDate
	 */
	public Date getDueDate() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(Long.parseLong(mData.get(AssignmentHelper.COLUMN_DUE).toString()));
		return cal.getTime();
	}

	/**
	 * @return the priority
	 */
	public Priority getPriority() {
		return (Priority) mData.get(AssignmentHelper.COLUMN_PRIORITY);
	}

	/**
	 * @return the isProject
	 */
	public boolean isProject() {
		return (Boolean) mData.get(AssignmentHelper.COLUMN_IS_PROJECT);
	}
	
	public long getId() {
		return (Long)mData.get(AssignmentHelper.COLUMN_ID);
	}
	
	public void setId(long id) {
		mData.put(AssignmentHelper.COLUMN_ID, id);
	}
	
	public ContentValues toContentValues() {
		ContentValues cv = new ContentValues();
		for (Iterator<String> iterator = mData.keySet().iterator(); iterator
				.hasNext();) {
			String key = iterator.next();
			Object value = mData.get(key);
			if (value instanceof Priority) {
				cv.put(key, ((Priority)value).ordinal());
			} else if (value instanceof Date) {
				cv.put(key, ((Date)value).getTime());
			} else if (value instanceof Boolean) {
				cv.put(key, ((Boolean)value) ? 1 : 0);
			} else {
				cv.put(key, value.toString());
			}
		}
		cv.put(AssignmentHelper.COLUMN_COURSE_ID, mParentCourse.getId());
		return cv;
	}

	private AssignmentData(CourseData parentCourse) {
		mParentCourse = parentCourse;
		mData = new HashMap<String, Object>();
	}
	
	public static class Builder {
		
		Context mContext;
		AssignmentData assignment;
		
		/**
		 * Construct a Builder to help create an {@link AssignmentData} 
		 * @param context The context from which this builder is called
		 * @param parentCourse The course to which this assignment will be attached
		 * @throws IllegalAccessException
		 */
		public Builder(Context context, CourseData parentCourse) throws IllegalAccessException {
			mContext = context;
			
			if (parentCourse.getId()==null) {
				CourseData course = new CourseHelper(mContext)
						.getCourseByName(parentCourse.getCourseTitle());
				if (course==null || course.getId()==null) {
					throw new IllegalArgumentException("parentCourse is either not recorded or" +
							" it doesn't have an ID.");
				} else {
					parentCourse.setId(course.getId());
				}
			}
			
			assignment = new AssignmentData(parentCourse);
		}
		
		public void setAssigned(Date assigned) {
			assignment.mData.put(AssignmentHelper.COLUMN_ASSIGNED, assigned.getTime());
		}
		
		public void setDueDate(Date due) {
			assignment.mData.put(AssignmentHelper.COLUMN_DUE, due.getTime());
		}
		
		public void setPriority(Priority priority) {
			assignment.mData.put(AssignmentHelper.COLUMN_PRIORITY, priority);
		}
		
		public void setIsProject(boolean isProject) {
			assignment.mData.put(AssignmentHelper.COLUMN_IS_PROJECT, isProject);
		}
		
		public void setName(String name) {
			assignment.mData.put(AssignmentHelper.COLUMN_NAME, name);
		}
		
		
		public AssignmentData build() {
			return assignment;
		}
	}

	public static AssignmentData buildFromCursor(Cursor cursor,Context context) {
		if (cursor.moveToFirst()) {
			long courseId = cursor.getLong(cursor.getColumnIndex(AssignmentHelper.COLUMN_COURSE_ID));
			CourseData parent = new CourseHelper(context).getCourseById(String.valueOf(courseId));
			AssignmentData data = new AssignmentData(parent);
			final int columns = cursor.getColumnCount();
			for (int i = 0; i < columns; i++) {
				String key = cursor.getColumnName(i);
				if (key.equals(AssignmentHelper.COLUMN_PRIORITY)) {
					data.mData.put(key,Priority.values()[cursor.getInt(i)]);
				} else {
					data.mData.put(key, cursor.getString(i));
				}
			}
			return data;
		}
		return null;
	}

}
